package kr.co.dayve.site.web.tag;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import java.io.IOException;

public class PagingTag extends BodyTagSupport {
    private String function;
    private int currentPage;
    private int totalRows;
    private int pageSize;
    private int pageLimit;
    private int rowsLimit;

    public PagingTag()
    {
        pageLimit = 10;
    }

    public int doStartTag()
    {
        return 1;
    }

    public int doEndTag()
    {
        StringBuffer uri = new StringBuffer();
        int totalPage = totalRows / pageSize;
        if(totalRows % pageSize > 0)
            totalPage++;
        int nowGroup = currentPage / pageLimit;
        int totalGroup = totalPage / pageLimit;
        if(totalPage % pageLimit > 0)
            totalGroup++;
        uri.append("&nbsp;&nbsp;");
        if(nowGroup <= 0){
            //이전 페이지가 없을 때
            //uri.append("<img src='/images/icon/btn_prev_1.gif' border='0'>");
            //uri.append("<");
        }else{
            //이전 페이지가 있을 때
            uri.append((new StringBuilder()).append("<a href=\"JavaScript:").append(function).append("('").append((nowGroup - 1) * 10).toString()).append((new StringBuilder()).append("','").append(nowGroup - 1).append("')\">").toString()).append("<img src='/images/icon/btn_prev_1.gif' border='0'></a>");
            uri.append("&nbsp;&nbsp;");
            uri.append((new StringBuilder()).append("<a href=\"JavaScript:").append(function).append("('").append(0).toString()).append((new StringBuilder()).append("','").append(0).append("')\"> &nbsp;").append(1).append("&nbsp; </a>").toString());
            uri.append("...");
        }

        int doStart = nowGroup * pageLimit;
        int doEnd = doStart + pageLimit;
        if(totalRows == 0)
            doEnd = 1;
        if(totalGroup - 1 == nowGroup)
            doEnd = totalPage;
        uri.append("&nbsp;&nbsp;");
        for(int i = doStart; i < doEnd; i++)
            if(i == currentPage)
                // 현재 페이지 표시
                uri.append((new StringBuilder()).append("&nbsp;<b>").append(i + 1).append("</b>&nbsp;").toString());
            else
                // 페이지 링크
                uri.append((new StringBuilder()).append("<a href=\"JavaScript:").append(function).append("('").append(i).toString()).append((new StringBuilder()).append("','").append(nowGroup).append("')\"> &nbsp;").append(i + 1).append("&nbsp; </a>").toString());

        uri.append("&nbsp;&nbsp;");
        if(nowGroup < totalGroup - 1){
            uri.append("...");
            uri.append((new StringBuilder()).append("<a href=\"JavaScript:").append(function).append("('").append(totalPage-1).toString()).append((new StringBuilder()).append("','").append(totalGroup-1).append("')\"> &nbsp;").append(totalPage).append("&nbsp; </a>").toString());
            uri.append("&nbsp;&nbsp;");
            // 다음 페이지가 있을 때
            uri.append((new StringBuilder()).append("<a href=\"JavaScript:").append(function).append("('").append((nowGroup + 1) * 10).toString()).append((new StringBuilder()).append("','").append(nowGroup + 1).append("')\">").toString()).append("<img src='/images/icon/btn_next_1.gif' border='0'></a>");
        }
        else{
            //다음 페이지가 없을 때
            //uri.append("<img src='/images/icon/btn_next_1.gif' border='0'>");
        }
        //uri.append(">");
        uri.append("&nbsp;&nbsp;");
        JspWriter out = pageContext.getOut();
        try
        {
            out.print(uri.toString());
        }
        catch(IOException e) { }
        return 6;
    }

    private int getTotalPage(int totalRow, int pageSize)
    {
        return totalRow <= 0 ? 0 : (int)Math.ceil(totalRow / pageSize) + totalRow % pageSize;
    }

    public void setPageLimit(int pageLimit)
    {
        this.pageLimit = pageLimit;
    }

    public void setCurrentPage(int currentPage)
    {
        this.currentPage = currentPage;
    }

    public void setTotalRows(int totalRows)
    {
        this.totalRows = totalRows;
    }

    public void setRowsLimit(int rowsLimit)
    {
        this.rowsLimit = rowsLimit;
    }

    public void setFunction(String function)
    {
        this.function = function;
    }

    public void setPageSize(int pageSize)
    {
        this.pageSize = pageSize;
    }
}
